<?php
namespace Controller;

require_once 'Controller.php';
require_once 'model/Annonce.php';
require_once 'model/Attribut.php';
require_once 'model/AnnonceAttribut.php';

use Controller\Controller;
use Model\Annonce;
use Model\Attribut;
use Model\AnnonceAttribut;
use Classes\Paginator;
use Classes\Filter;
use Classes\Upload;

class AnnonceController extends Controller {
    public function __construct() {
        $this->name = 'annonce';
    }
    
    public function indexView() {
        $this->headerTitle = 'Gestion des annonces';        
        $filterObject = new Filter();
        $annonceObject = new Annonce();
        $attributObject = new Attribut();
        
        // Filtre pour les statuts
        $statut = array(
            'all' => $attributObject->getAttributByAlias('statut')->predefined_value, // Statuts disponibles
            'current' => null // Statut des annonces à afficher(null = tous les statuts)
        );        
        $filterObject->setStatut($statut);
        
        // Filtre pour les recherches
        $attributsIdsToSearch = $attributObject->getAllSearchIds();
        $filterObject->setSearch($attributsIdsToSearch);
        
        // Récupération de la clause where
        $where = $filterObject->getWhere();
        
        $items_total = count($annonceObject->fetchAll($where));
        $paginator = new Paginator($items_total, 10);
        $paginator->page_nav_class = "btn btn-xs btn-default";
        $paginator->page_nav_desabled_class = "btn btn-xs btn-primary";
        
        $variables = array(
            'annonces' => $annonceObject->fetchAll($where, null, null, $paginator->limit_start, $paginator->items_per_page),
            'cols' => $attributObject->fetchAllCol(),
            'searchField' => $filterObject->displaySearchField($filterObject->searchText),
            'pager' => $paginator->pager(),
            'start_num' => $paginator->start_num,
            'beginForm' => $paginator->beginForm(),
            'endForm' => $paginator->endForm()
        );   
        $this->displayView('index', $variables);
    }
    
    public function addView() {
        $this->headerTitle = 'Ajout d\'une annonce';
        $annonceObject = new Annonce();
        $returnCode = array();
        if($this->getPost()) {
            $post = $this->getPost();
            $data = $post['data'];
            $data['statut'] = $post['attrData']['statut']; // Assignation de l'attribut statut au champ satut
            $attrData = $post['attrData'];
            if($this->getFiles()) {
                $files = $this->getFiles();
                $attrData[Upload::FILES_INDEX] = $files['attrData'];
            }
            $returnCode = $annonceObject->saveAnnonce($data, $attrData);            
            if(empty($returnCode)) {
                $this->redirect($this->name);
            }
        }
        
        $variables = array(
            'annonce' => $annonceObject->newAnnonce(),
            'returnCode' => $returnCode
        );
        $this->displayView('edit', $variables);
    }
    
    public function editView() {
        $this->headerTitle = 'Edition d\'une annonce';
        $annonceObject = new Annonce();
        $id_annonce = (int) $_GET['id'];
        $returnCode = array();
        if($this->getPost()) {
            $post = $this->getPost();
            $data = $post['data'];
            $data['statut'] = $post['attrData']['statut']; // Assignation de l'attribut statut au champ satut
            $attrData = $post['attrData'];  
            if($this->getFiles()) {
                $files = $this->getFiles();
                $attrData[Upload::FILES_INDEX] = $files['attrData'];
            }            
            $returnCode = $annonceObject->saveAnnonce($data, $attrData);            
            if(empty($returnCode)) {
                $this->redirect($this->name);
            }
        }
        
        $variables = array(
            'annonce' => $annonceObject->getAnnonce($id_annonce, true),
            'returnCode' => $returnCode
        );
        $this->displayView('edit', $variables);
    }
    
    public function deleteView() {
        $id_annonce = (int) $_GET['id'];
        $annonceObject = new Annonce();
        if($annonceObject->deleteAnnonce($id_annonce)) {
            $this->redirect($this->name);
        }
    }
    
    public function deletefileView() {        
        if($this->getPost()) {
            $post = $this->getPost();
            $filename = $post['filename'];
            
            // Suppression de l'image
            $upload = new Upload();
            $upload->deleteFile($filename);
            
            // Mis à jour de la valeur dans la base de données
            $annonceAttributObject = new AnnonceAttribut();
            $ids = $annonceAttributObject->getAnnonceAttributByValue($filename);
            $attrData = array(
                'id_annonce' => $ids[0]->id_annonce,
                'id_attribut' => $ids[0]->id_attribut,
                'value' => ''
            );
            $annonceAttributObject->saveAnnonceAttribut($attrData);
        }
    }
    
    public function changestatutView() {
        if($this->getPost()) {
            $post = $this->getPost();
            $id_annonce = (int)$post['id_annonce'];
            $statut = (int)$post['statut'];
            
            $annonceObject = new Annonce();
            $annonceObject->changeStatut($id_annonce, $statut);
            
            // Changement de la valeur de l'attribut
            $attributObject = new Attribut();            
            $id_attribut = $attributObject->getAttributByAlias('statut')->id_attribut;
            
            $annonceAttributObject = new AnnonceAttribut();
            $data = array(
                'id_annonce' => $id_annonce,
                'id_attribut' => $id_attribut,
                'value' => $statut
            );
            $annonceAttributObject->saveAnnonceAttribut($data);
        }
        
    }
}